<?php
/* Category controller 
** thaodx@gmail.com
*/
if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Category extends CI_Controller {
	// Layout used in this controller
    public $layout_view = 'layout/9gag';

	public function index($cateCode = '', $page = 0, $lastId = 0)
	{
		/*$page = $this->uri->segment(4, 1);
		if(strstr($page, '-')){
			$arrSegment = explode('-', $page);
			$page = intval();
		}*/

		$arrCate = $this->config->config['categories'];

		$cateInfo = array();
		foreach($arrCate as $id => $info){
			if($info[0] == 'category/'.$cateCode){
				$cateInfo = array('id' => $id, 'fullcode' => $info[0], 'title' => $info[1]);
				break;
			}
		}
		$this->load->model('Post_model', 'cateModel');
		$this->load->helper(array('datetime', 'permalink', 'social', 'image'));
		#$this->load->library('pagination');
		$latestPost = $this->cateModel->getPostByCate($cateInfo['id'], $page, CATE_LIMIT, $lastId);
		$additionTitle = '';
		if($page > 0){
			$additionTitle = ' - Trang '. ($page / CATE_LIMIT + 1);
		}
		$this->layout->title(SITE_TITLE . TITLE_SEPARATOR . $cateInfo['title'].' tổng hợp'.$additionTitle); // Set page title
		$this->layout->meta('<meta http-equiv="REFRESH" content="1800" />');
       	$data = array('data' => $latestPost, 'page' => $page, 'cid' => $cateInfo['id'], 'fullCateCode' => $cateInfo['fullcode']);
       	// sidebar
       	$sidebar = $this->layout->renderBlock('block/cate-side-bar');
       	$this->layout->sidebar($sidebar);
       	// paginate
       /*
       	$paging['base_url'] = BASE_URL.'/page/';
		$paging['total_rows'] = 4000;
		$paging['per_page'] = HOME_LIMIT;
		$paging['uri_segment'] = 2;
		$this->pagination->initialize($paging);

		$data['paging'] = $this->pagination->create_links();
		*/

       	$this->layout->view('category_index', $data);// Render view and layout
	}
}